system-controller: add outputname field to layers
[profile/ivi/murphy.git] / packaging.in / murphy.lua
1 with_system_controller = false
2 with_amb = false
3 verbose = 0
4
5 m = murphy.get()
6
7 -- try loading the various logging plugins
8 m:try_load_plugin('systemd')
9 m:try_load_plugin('dlog')
10
11 -- load the console plugin
12 m:try_load_plugin('console')
13
14 m:try_load_plugin('console.disabled', 'webconsole', {
15                   address = 'wsck:127.0.0.1:3000/murphy',
16                   httpdir = '/usr/share/murphy/webconsole' });
17
18 -- load the dbus plugin
19 if m:plugin_exists('dbus') then
20     m:load_plugin('dbus')
21 end
22
23 -- load the native resource plugin
24 if m:plugin_exists('resource-native') then
25     m:load_plugin('resource-native')
26     m:info("native resource plugin loaded")
27 else
28     m:info("No native resource plugin found...")
29 end
30
31 -- load the dbus resource plugin
32 m:try_load_plugin('resource-dbus', {
33     dbus_bus = "system",
34     dbus_service = "org.Murphy",
35     dbus_track = true,
36     default_zone = "driver",
37     default_class = "implicit"
38 })
39
40 -- load the domain control plugin
41 if m:plugin_exists('domain-control') then
42     m:load_plugin('domain-control')
43 else
44     m:info("No domain-control plugin found...")
45 end
46
47 if m:plugin_exists('glib') then
48     m:load_plugin('glib')
49 else
50     m:info("No glib plugin found...")
51 end
52
53 -- load the AMB plugin
54 if m:plugin_exists('amb') then
55     m:try_load_plugin('amb')
56
57     if builtin.method.amb_initiate and
58        builtin.method.amb_update
59     then
60         with_amb = true
61     end
62 else
63     m:info("No amb plugin found...")
64 end
65
66 -- load the ASM resource plugin
67 if m:plugin_exists('resource-asm') then
68     m:try_load_plugin('resource-asm', {
69         zone = "driver",
70         share_mmplayer = "player:AVP,mandatory,exclusive,strict",
71         ignored_argv0 = "WebProcess"
72     })
73 else
74     m:info("No audio session manager plugin found...")
75 end
76
77 if m:plugin_exists('system-controller') then
78     with_system_controller = true
79 elseif m:plugin_exists('ivi-resource-manager') then
80     m:load_plugin('ivi-resource-manager')
81     with_system_controller = false
82 end
83
84 -- define application classes
85 application_class {
86     name     = "interrupt",
87     priority = 99,
88     modal    = true ,
89     share    = false,
90     order    = "fifo"
91 }
92
93 application_class {
94     name     = "emergency",
95     priority = 80,
96     modal    = false,
97     share    = false,
98     order    = "fifo"
99 }
100 application_class {
101     name     = "system",
102     priority = 52,
103     modal    = false,
104     share    = true,
105     order    = "lifo"
106 }
107 application_class {
108     name     = "alert",
109     priority = 51,
110     modal    = false,
111     share    = false,
112     order    = "fifo"
113 }
114
115 application_class {
116     name     = "navigator",
117     priority = 50,
118     modal    = false,
119     share    = true,
120     order    = "fifo"
121 }
122
123 application_class {
124     name     = "phone",
125     priority = 6 ,
126     modal    = false,
127     share    = true ,
128     order    = "lifo"
129 }
130 application_class {
131     name     = "camera",
132     priority = 5,
133     modal    = false,
134     share    = false,
135     order    = "lifo"
136 }
137
138 application_class { name="event"    , priority=4 , modal=false, share=true , order="fifo" }
139 application_class { name="game"     , priority=3 , modal=false, share=false, order="lifo" }
140 --# doesn't need to be created here, ivi-resource-manager creates it if loaded
141 --#application_class { name="basic"    , priority=2 , modal=false, share=false, order="lifo" }
142 application_class { name="player"   , priority=1 , modal=false, share=true , order="lifo" }
143 application_class { name="implicit" , priority=0 , modal=false, share=false, order="lifo" }
144
145 -- define zone attributes
146 zone.attributes {
147     type = {mdb.string, "common", "rw"},
148     location = {mdb.string, "anywhere", "rw"}
149 }
150
151 -- define zones
152 zone {
153      name = "driver",
154      attributes = {
155          type = "common",
156          location = "front-left"
157      }
158 }
159
160 zone {
161      name = "passanger1",
162      attributes = {
163          type = "private",
164          location = "front-right"
165      }
166 }
167
168 zone {
169      name = "passanger2",
170      attributes = {
171          type = "private",
172          location = "back-left"
173      }
174 }
175
176 zone {
177      name = "passanger3",
178      attributes = {
179          type = "private",
180          location = "back-right"
181      }
182 }
183
184 zone {
185      name = "passanger4",
186      attributes = {
187          type = "private",
188          location = "back-left"
189      }
190 }
191
192
193 -- define resource classes
194 if not m:plugin_exists('ivi-resource-manager') and
195    not with_system_controller
196 then
197    resource.class {
198         name = "audio_playback",
199         shareable = true,
200         attributes = {
201             role = { mdb.string, "music", "rw" },
202             pid = { mdb.string, "<unknown>", "rw" },
203             policy = { mdb.string, "relaxed", "rw" }
204         }
205    }
206 end
207
208 resource.class {
209      name = "audio_recording",
210      shareable = true,
211      attributes = {
212          role   = { mdb.string, "music"    , "rw" },
213          pid    = { mdb.string, "<unknown>", "rw" },
214          policy = { mdb.string, "relaxed"  , "rw" }
215      }
216 }
217
218 resource.class {
219      name = "video_playback",
220      shareable = false,
221 }
222
223 resource.class {
224      name = "video_recording",
225      shareable = false
226 }
227
228 resource.class {
229      name = "speech_recognition",
230      shareable = true
231 }
232
233 resource.class {
234      name = "speech_synthesis",
235      shareable = true
236 }
237
238 -- PulseAudio volume context
239 mdb.table {
240     name = "volume_context",
241     index = { "id" },
242     create = true,
243     columns = {
244         { "id", mdb.unsigned },
245         { "value", mdb.string, 64 },
246     }
247 }
248
249 -- put default volume context to the table
250 mdb.table.volume_context:insert({ id = 1, value = "default" })
251
252 if not m:plugin_exists('ivi-resource-manager') and
253    not with_system_controller
254 then
255     resource.method.veto = {
256         function(zone, rset, grant, owners, req_set)
257             return true
258          end
259     }
260 end
261
262 -- test for creating selections
263 mdb.select {
264            name = "audio_owner",
265            table = "audio_playback_owner",
266            columns = {"application_class"},
267            condition = "zone_name = 'driver'"
268 }
269
270 mdb.select {
271            name = "vehicle_speed",
272            table = "amb_vehicle_speed",
273            columns = {"value"},
274            condition = "key = 'VehicleSpeed'"
275 }
276
277 element.lua {
278    name    = "speed2volume",
279    inputs  = { speed = mdb.select.vehicle_speed, param = 9 },
280    outputs = {  mdb.table { name = "speedvol",
281                             index = {"zone", "device"},
282                             columns = {{"zone", mdb.string, 16},
283                                        {"device", mdb.string, 16},
284                                        {"value", mdb.floating}},
285                             create = true
286                            }
287              },
288    oldvolume = 0.0,
289    update  = function(self)
290                 speed = self.inputs.speed.single_value
291                 if (speed) then
292                     volume = (speed - 144.0) / 7.0
293                 else
294                     volume = 0.0
295                 end
296                 diff = volume - self.oldvolume
297                 if (diff*diff > self.inputs.param) then
298                     print("*** element "..self.name.." update "..volume)
299                     self.oldvolume = volume
300                     mdb.table.speedvol:replace({zone = "driver", device = "speakers", value = volume})
301                 end
302              end
303 }
304
305 mdb.select {
306     name = "amb_state",
307     table = "amb_state",
308     columns = { "state" },
309     condition = "id = 0"
310 }
311
312 -- Night mode processing chain
313
314 mdb.select {
315     name = "exterior_brightness",
316     table = "amb_exterior_brightness",
317     columns = { "value" },
318     condition = "key = 'ExteriorBrightness'"
319 }
320
321 element.lua {
322     name    = "nightmode",
323     inputs  = { brightness = mdb.select.exterior_brightness },
324     oldmode = -1;
325     outputs = {
326         mdb.table {
327             name = "amb_nightmode",
328             index = { "id" },
329             create = true,
330             columns = {
331                 { "id", mdb.unsigned },
332                 { "night_mode", mdb.unsigned }
333             }
334         }
335     },
336     update = function(self)
337         -- This is a trivial function to calculate night mode. Later, we will
338         -- need a better threshold value and hysteresis to prevent oscillation.
339
340         brightness = self.inputs.brightness.single_value
341
342         if not brightness then
343             return
344         end
345
346         print("*** element "..self.name.." update brightness: "..brightness)
347
348         if brightness > 300 then
349             mode = 0
350         else
351             mode = 1
352         end
353
354         print("*** resulting mode: ".. mode)
355
356         if not (mode == self.oldmode) then
357             mdb.table.amb_nightmode:replace({ id = 0, night_mode = mode })
358         end
359
360         self.oldmode = mode
361     end
362 }
363
364 mdb.select {
365     name = "select_night_mode",
366     table = "amb_nightmode",
367     columns = { "night_mode" },
368     condition = "id = 0"
369 }
370
371 if with_amb then
372     sink.lua {
373         name = "night_mode",
374         inputs = { NightMode = mdb.select.select_night_mode,
375                    amb_state = mdb.select.amb_state },
376         property = "NightMode",
377         type = "b",
378         initiate = builtin.method.amb_initiate,
379         update = builtin.method.amb_update
380     }
381 end
382
383 -- Night mode general handlers
384
385 if with_system_controller then
386     sink.lua {
387         name = "nightmode_homescreen",
388         inputs = { owner = mdb.select.select_night_mode },
389         initiate = function(self)
390                 -- data = mdb.select.select_night_mode.single_value
391                 return true
392             end,
393         update = function(self)
394                 send_night_mode_to_home_screen()
395             end
396     }
397 end
398
399 -- Driving mode processing chain
400
401 element.lua {
402     name    = "drivingmode",
403     inputs  = { speed = mdb.select.vehicle_speed },
404     oldmode = -1;
405     outputs = {
406         mdb.table {
407             name = "amb_drivingmode",
408             index = { "id" },
409             create = true,
410             columns = {
411                 { "id", mdb.unsigned },
412                 { "driving_mode", mdb.unsigned }
413             }
414         }
415     },
416     update = function(self)
417
418         speed = self.inputs.speed.single_value
419
420         if not speed then
421             return
422         end
423
424         if speed == 0 then
425             mode = 0
426         else
427             mode = 1
428         end
429
430         if not (mode == self.oldmode) then
431             mdb.table.amb_drivingmode:replace({ id = 0, driving_mode = mode })
432         end
433
434         self.oldmode = mode
435     end
436 }
437
438 mdb.select {
439     name = "select_driving_mode",
440     table = "amb_drivingmode",
441     columns = { "driving_mode" },
442     condition = "id = 0"
443 }
444
445 if with_amb then
446     sink.lua {
447         name = "driving_mode",
448         inputs = { DrivingMode = mdb.select.select_driving_mode,
449                    amb_state = mdb.select.amb_state },
450         property = "DrivingMode",
451         type = "u",
452         initiate = builtin.method.amb_initiate,
453         update = builtin.method.amb_update
454     }
455 end
456
457 -- turn signals (left, right)
458
459 mdb.select {
460     name = "winker",
461     table = "amb_turn_signal",
462     columns = { "value" },
463     condition = "key = 'TurnSignal'"
464 }
465
466 -- define three categories
467
468 mdb.select {
469     name = "undefined_applications",
470     table = "aul_applications",
471     columns = { "appid" },
472     condition = "category = '<undefined>'"
473 }
474
475 mdb.select {
476     name = "basic_applications",
477     table = "aul_applications",
478     columns = { "appid" },
479     condition = "category = 'basic'"
480 }
481
482 mdb.select {
483     name = "entertainment_applications",
484     table = "aul_applications",
485     columns = { "appid" },
486     condition = "category = 'entertainment'"
487 }
488
489 function ft(t)
490     -- filter the object garbage out of the tables
491     ret = {}
492
493     for k,v in pairs(t) do
494         if k ~= "userdata" and k ~= "new" then
495             ret[k] = v
496         end
497     end
498
499     return ret
500 end
501
502 function getApplication(appid)
503     local conf = nil
504
505     -- find the correct local application definition
506
507     for k,v in pairs(ft(application)) do
508         if appid == v.appid then
509             conf = v
510             break
511         end
512     end
513
514     return conf
515 end
516
517 function regulateApplications(t, regulation)
518     for k,v in pairs(ft(t)) do
519
520         -- iterate through the undefined and entertainment apps, see if
521         -- they have been overruled in local config
522
523         local conf = getApplication(v.appid)
524
525         if conf and conf.resource_class ~= "player" then
526             -- override, don't disable
527             resmgr:disable_screen_by_appid("*", "*", v.appid, false, false)
528         else
529             resmgr:disable_screen_by_appid("*", "*", v.appid, regulation == 1, false)
530         end
531     end
532     resource.method.recalc("driver")
533 end
534
535 -- regulation (on), use "select_driving_mode"
536
537 sink.lua {
538     name = "driving_regulation",
539     inputs = { owner = mdb.select.select_driving_mode },
540     initiate = function(self)
541         -- local data = mdb.select.select_driving_mode.single_value
542         return true
543     end,
544     update = function(self)
545         local data = mdb.select.select_driving_mode.single_value
546
547         if verbose > 1 then
548             print("Driving mode updated: " .. tostring(data))
549         end
550
551         if not sc then
552             return true
553         end
554
555         -- tell homescreen that driving mode was updated
556         send_driving_mode_to_home_screen()
557
558         regulateApplications(ft(mdb.select.entertainment_applications), data)
559         regulateApplications(ft(mdb.select.undefined_applications), data)
560
561         return true
562     end
563 }
564 --[[
565 sink.lua {
566     name = "regulated_app_change",
567     inputs = { undef = mdb.select.undefined_applications,
568                entertainment = mdb.select.entertainment_applications },
569     initiate = function(self)
570         return true
571     end,
572     update = function(self)
573         local data = mdb.select.select_driving_mode.single_value
574
575         if not sc then
576             return
577         end
578
579         if verbose > 1 then
580             print("regulated application list was changed")
581         end
582
583         regulateApplications(ft(mdb.select.entertainment_applications), data)
584         regulateApplications(ft(mdb.select.undefined_applications), data)
585
586         return true
587     end
588 }
589 --]]
590
591 -- shift position (parking, reverse, other)
592
593 mdb.select {
594     name = "gear_position",
595     table = "amb_gear_position",
596     columns = { "value" },
597     condition = "key = 'GearPosition'"
598 }
599
600 -- cameras (back, front, left, right)
601
602 element.lua {
603     name    = "camera_state",
604     inputs  = { winker = mdb.select.winker, gear = mdb.select.gear_position },
605     oldmode = -1;
606     outputs = {
607         mdb.table {
608             name = "target_camera_state",
609             index = { "id" },
610             create = true,
611             columns = {
612                 { "id", mdb.unsigned },
613                 { "front_camera", mdb.unsigned },
614                 { "back_camera", mdb.unsigned },
615                 { "right_camera", mdb.unsigned },
616                 { "left_camera", mdb.unsigned }
617             }
618         }
619     },
620     update = function(self)
621
622         front_camera = 0
623         back_camera = 0
624         right_camera = 0
625         left_camera = 0
626
627         if self.inputs.gear == 128 then
628             back_camera = 1
629         elseif self.inputs.winker == 1 then
630             right_camera = 1
631         elseif self.inputs.winker == 2 then
632             left_camera = 1
633         end
634
635         mdb.table.target_camera_state:replace({ id = 0, front_camera = front_camera, back_camera = back_camera, right_camera = right_camera, left_camera = left_camera })
636
637     end
638 }
639
640 -- system controller test setup
641
642 if not with_system_controller then
643    -- ok, we should have 'audio_playback' defined by now
644    m:try_load_plugin('telephony')
645    return
646 end
647
648 m:load_plugin('system-controller')
649
650 window_manager_operation_names = {
651     [1] = "create",
652     [2] = "destroy"
653 }
654
655 function window_manager_operation_name(oper)
656     local name = window_manager_operation_names[oper]
657     if name then return name end
658     return "<unknown " .. tostring(oper) .. ">"
659 end
660
661 window_operation_names = {
662     [1] = "create",
663     [2] = "destroy",
664     [3] = "name_change",
665     [4] = "visible",
666     [5] = "configure",
667     [6] = "active",
668     [7] = "map",
669     [8] = "hint"
670 }
671
672 function window_operation_name(oper)
673     local name = window_operation_names[oper]
674     if name then return name end
675     return "<unknown " .. tostring(oper) .. ">"
676 end
677
678 layer_operation_names = {
679     [1] = "create",
680     [2] = "destroy",
681     [3] = "visible"
682 }
683
684 function layer_operation_name(oper)
685     local name = layer_operation_names[oper]
686     if name then return name end
687     return "<unknown " .. tostring(oper) .. ">"
688 end
689
690 input_manager_operation_names = {
691     [1] = "create",
692     [2] = "destroy",
693     [3] = "ready"
694 }
695
696 function input_manager_operation_name(oper)
697     local name = input_manager_operation_names[oper]
698     if name then return name end
699     return "<unknown " .. tostring(oper) .. ">"
700 end
701
702 input_operation_names = {
703     [1] = "create",
704     [2] = "destroy",
705     [3] = "update"
706 }
707
708 function input_operation_name(oper)
709     local name = input_operation_names[oper]
710     if name then return name end
711     return "<unknown " .. tostring(oper) .. ">"
712 end
713
714 code_operation_names = {
715     [1] = "create",
716     [2] = "destroy",
717     [3] = "state_change"
718 }
719
720 function code_operation_name(oper)
721     local name = code_operation_names[oper]
722     if name then return name end
723     return "<unknown " .. tostring(oper) .. ">"
724 end
725
726 command_names = {
727     [0x00001] = "send_appid",
728     [0x10001] = "create",
729     [0x10002] = "destroy",
730     [0x10003] = "show",
731     [0x10004] = "hide",
732     [0x10005] = "move",
733     [0x10006] = "change_active",
734     [0x10007] = "change_layer",
735     [0x10008] = "change_attr",
736     [0x10009] = "name",
737     [0x10011] = "map_thumb",
738     [0x10012] = "unmap_thumb",
739     [0x10020] = "show layer",
740     [0x10021] = "hide_layer",
741     [0x10022] = "change_layer_attr",
742     [0x20001] = "add_input",
743     [0x20002] = "del_input",
744     [0x20003] = "send_input",
745     [0x40001] = "acquire_res",
746     [0x40002] = "release_res",
747     [0x40003] = "deprive_res",
748     [0x40004] = "waiting_res",
749     [0x40005] = "revert_res",
750     [0x40011] = "create_res",
751     [0x40012] = "destroy_res",
752     [0x50001] = "set_region",
753     [0x50002] = "unset_region",
754     [0x60001] = "change_state"
755 }
756
757 function command_name(command)
758     local name = command_names[command]
759     if name then return name end
760     return "<unknown " .. tostring(command) .. ">"
761 end
762
763 input_layer = {
764    [3] = true, -- input
765    [4] = true, -- touch
766    [5] = true  -- cursor
767 }
768
769 -- some day this should be merged with wmgr.layers
770 ico_layer_type = {
771    [1] = 0x1000, -- background
772    [2] = 0x2000, -- application
773    [3] = 0x4000, -- input
774    [4] = 0xa000, -- touch
775    [5] = 0xb000, -- cursor
776    [6] = 0xc000, -- startup
777    [7] = 0x3000  -- fullscreen
778 }
779
780 resmgr = resource_manager {
781   screen_event_handler = function(self, ev)
782                              local event = ev.event
783                              local surface = ev.surface
784
785                              if event == "init" then
786                                  if verbose > 0 then
787                                      print("*** init screen resource allocation -- disable all 'player'")
788                                  end
789                                  resmgr:disable_audio_by_appid("*", "player", "*", true, false)
790                              elseif event == "preallocate" then
791                                  if verbose > 0 then
792                                      print("*** preallocate screen resource "..
793                                            "for '" .. ev.appid .. "' -- enable 'player', if any")
794                                  end
795                                  resmgr:disable_audio_by_appid("*", "player", ev.appid, false, false)
796                              elseif event == "grant" then
797                                  if verbose > 0 then
798                                     print("*** make visible surface "..surface)
799                                  end
800                                  local a = animation({})
801                                  local r = m:JSON({surface = surface,
802                                                    visible = 1,
803                                                    raise   = 1})
804                                  wmgr:window_request(r,a,0)
805                              elseif event == "revoke" then
806                                  if verbose > 0 then
807                                     print("*** hide surface "..surface)
808                                  end
809                                  local a = animation({})
810                                  local r = m:JSON({surface = ev.surface,
811                                                    visible = 0})
812                                  wmgr:window_request(r,a,0)
813
814                              elseif event == "create" then
815
816                                 if verbose > 0 then
817                                     print("*** screen resource event: " ..
818                                           tostring(ev))
819                                 end
820
821                                 local regulation = mdb.select.select_driving_mode.single_value
822
823                                 if regulation == 1 then
824
825                                     local blacklisted = false
826
827                                     -- applications which have their category set to "entertainment"
828                                     -- or "undefined" are blacklisted, meaning they should be regulated
829
830                                     for i,v in pairs(ft(mdb.select.undefined_applications)) do
831                                         if v.appid == ev.appid then
832                                             if verbose > 0 then
833                                                 print(ev.appid .. " was blacklisted (undefined)")
834                                             end
835                                             blacklisted = true
836                                             break
837                                         end
838                                     end
839
840                                     if not blacklisted then
841                                         for i,v in pairs(ft(mdb.select.entertainment_applications)) do
842                                             if v.appid == ev.appid then
843                                                 if verbose > 0 then
844                                                     print(ev.appid .. " was blacklisted (entertainment)")
845                                                 end
846                                                 blacklisted = true
847                                                 break
848                                             end
849                                         end
850                                     end
851
852                                     -- our local application config, which takes precedence
853                                     local conf = getApplication(ev.appid)
854
855                                     -- disable only non-whitelisted applications
856                                     if not conf or conf.resource_class == "player" then
857                                         if blacklisted then
858                                             if verbose > 0 then
859                                                 print("disabling screen for " .. ev.appid)
860                                             end
861                                             resmgr:disable_screen_by_appid("*", "*", ev.appid, true, true)
862                                         end
863                                     end
864                                 end
865
866                              elseif event == "destroy" then
867                                if verbose > 0 then
868                                     print("*** screen resource event: " ..
869                                           tostring(ev))
870                                  end
871                              else
872                                  if verbose > 0 then
873                                     print("*** screen resource event: " ..
874                                           tostring(ev))
875                                  end
876                              end
877                          end,
878   audio_event_handler = function(self, ev)
879                              local event = ev.event
880                              local appid = ev.appid
881                              local audioid = ev.audioid
882
883                              if event == "grant" then
884                                  if verbose > 0 then
885                                     print("*** grant audio to "..appid..
886                                           " ("..audioid..") in '" ..
887                                           ev.zone .. "' zone")
888                                  end
889                              elseif event == "revoke" then
890                                  if verbose > 0 then
891                                     print("*** revoke audio from "..appid..
892                                           " ("..audioid..") in '" ..
893                                           ev.zone .. "' zone")
894                                  end
895                              else
896                                  if verbose > 0 then
897                                     print("*** audio resource event: " ..
898                                           tostring(ev))
899                                  end
900                              end
901                         end
902 }
903
904 resclnt = resource_client {}
905
906 wmgr = window_manager {
907   geometry = function(self, w,h, v)
908                   if type(v) == "function" then
909                       return v(w,h)
910                   end
911                   return v
912              end,
913
914   application = function(self, appid)
915                      if appid then
916                          local app = application_lookup(appid)
917                          if not app then
918                              app = application_lookup("default")
919                          end
920                          return app
921                      end
922                      return { privileges = {screen="none", audio="none"} }
923                 end,
924
925   output_order = { 1, 0 },
926
927   outputs = { { name  = "Mid",
928                 id    = 1,
929                 zone  = "driver",
930                 areas = { Full = {
931                               id     = 20,
932                               pos_x  = 0,
933                               pos_y  = 0,
934                               width  = function(w,h) return w end,
935                               height = function(w,h) return h end
936                           },
937                           Left = {
938                               id     = 21,
939                               pos_x  = 0,
940                               pos_y  = 0,
941                               width  = 320,
942                               height = function(w,h) return h end
943                           },
944                           Right = {
945                               id     = 22,
946                               pos_x  = function(w,h) return w-320 end,
947                               pos_y  = 0,
948                               width  = 320,
949                               height = function(w,h) return h end
950                           }
951                         }
952                },
953                { name  = "Center",
954                  id    = 4,
955                  zone  = "driver",
956                  areas = { Status = {
957                               id     = 0,
958                               pos_x  = 0,
959                               pos_y  = 0,
960                               width  = function(w,h) return w end,
961                               height = 64
962                            },
963                            Full = {
964                               id     = 1,
965                               pos_x  = 0,
966                               pos_y  = 64,
967                               width  = function(w,h) return w end,
968                               height = function(w,h) return h-64-128 end
969                            },
970                            Upper = {
971                               id     = 2,
972                               pos_x  = 0,
973                               pos_y  = 64,
974                               width  = function(w,h) return w end,
975                               height = function(w,h) return (h-64-128)/2 end
976                            },
977                            Lower = {
978                               id     = 3,
979                               pos_x  = 0,
980                               pos_y  = function(w,h) return (h-64-128)/2+64 end,
981                               width  = function(w,h) return w end,
982                               height = function(w,h) return (h-64-128)/2 end
983                            },
984                            UpperLeft = {
985                               id     = 4,
986                               pos_x  = 0,
987                               pos_y  = 64,
988                               width  = function(w,h) return w/2 end,
989                               height = function(w,h) return (h-64-128)/2 end
990                            },
991                            UpperRight = {
992                               id     = 5,
993                               pos_x  = function(w,h) return w/2 end,
994                               pos_y  = 64,
995                               width  = function(w,h) return w/2 end,
996                               height = function(w,h) return (h-64-128)/2 end
997                            },
998                            LowerLeft = {
999                               id     = 6,
1000                               pos_x  = 0,
1001                               pos_y  = function(w,h) return (h-64-128/2)+64 end,
1002                               width  = function(w,h) return w/2 end,
1003                               height = function(w,h) return (h-64-128)/2 end
1004                            },
1005                            LowerRight = {
1006                               id     = 7,
1007                               pos_x  = function(w,h) return w/2 end,
1008                               pos_y  = function(w,h) return (h-64-128/2)+64 end,
1009                               width  = function(w,h) return w/2 end,
1010                               height = function(w,h) return (h-64-128)/2 end
1011                            },
1012                            SysApp = {
1013                               id     = 8,
1014                               pos_x  = 0,
1015                               pos_y  = 64,
1016                               width  = function(w,h) return w end,
1017                               height = function(w,h) return h-64-128 end
1018                            },
1019                            ["SysApp.Left"] = {
1020                               id     = 9,
1021                               pos_x  = 0,
1022                               pos_y  = 64,
1023                               width  = function(w,h) return w/2-181 end,
1024                               height = function(w,h) return h-64-128 end
1025                            },
1026                            ["SysApp.Right"] = {
1027                               id     = 10,
1028                               pos_x  = function(w,h) return w/2+181 end,
1029                               pos_y  = 64,
1030                               width  = function(w,h) return w/2-181 end,
1031                               height = function(w,h) return h-64-128 end
1032                            },
1033                            Control = {
1034                               id     = 11,
1035                               pos_x  = 0,
1036                               pos_y  = function(w,h) return h-128 end,
1037                               width  = function(w,h) return w end,
1038                               height = 128
1039                            },
1040                         }
1041               }
1042   },
1043              --    id   name            type  output
1044   layers = { {      0, "Background"   ,    1, "Center" },
1045              {      1, "Application"  ,    2, "Center" },
1046              {      2, "Softkeyboard" ,    4, "Center" },
1047              {      3, "HomeScreen"   ,    2, "Center" },
1048              {      4, "ControlBar"   ,    2, "Center" },
1049              {      5, "InterruptApp" ,    2, "Center" },
1050              {      6, "OnScreen"     ,    2, "Center" },
1051              {    101, "Input"        ,    3, "Center" },
1052              {    102, "Cursor"       ,    5, "Center" },
1053              {    103, "Startup"      ,    6, "Center" },
1054              { 0x1000, "Background"   ,    1, "Center" },
1055              { 0x2000, "Normal"       ,    2, "Center" },
1056              { 0x3000, "Fullscreen"   ,    7, "Center" },
1057              { 0x4000, "InputPanel"   ,    3, "Center" },
1058              { 0xA000, "Touch"        ,    4, "Center" },
1059              { 0xB000, "Cursor"       ,    5, "Center" },
1060              { 0xC000, "Startup"      ,    6, "Center" }
1061   },
1062
1063
1064   manager_update = function(self, oper)
1065                        if verbose > 0 then
1066                            print("### <== WINDOW MANAGER UPDATE:" ..
1067                                  window_manager_operation_name(oper))
1068                        end
1069                        if oper == 1 then
1070                            local wumask = window_mask { raise   = true,
1071                                                         visible = true,
1072                                                         active  = true }
1073                            local wrmask = window_mask { active  = true,
1074                                                         layer   = true }
1075                            local lumask = layer_mask  { visible = true }
1076                            local lrmask = layer_mask  { visible = true }
1077                            local req = m:JSON({
1078                                passthrough_window_update  = wumask:tointeger(),
1079                                passthrough_window_request = wrmask:tointeger(),
1080                                passthrough_layer_update   = lumask:tointeger(),
1081                                passthrough_layer_request  = lrmask:tointeger()
1082                            })
1083                            self:manager_request(req)
1084                        end
1085                    end,
1086
1087   window_update = function(self, oper, win, mask)
1088                       if verbose > 0 then
1089                           print("### <== WINDOW UPDATE oper:" ..
1090                                 window_operation_name(oper) ..
1091                                 " mask: " .. tostring(mask))
1092                           if verbose > 1 then
1093                               print(win)
1094                           end
1095                       end
1096
1097                       local arg = m:JSON({ surface = win.surface,
1098                                            winname = win.name,
1099                       })
1100                       local command = 0
1101
1102                       if oper == 1 then -- create
1103                            local layertype = win.layertype
1104                            if layertype and input_layer[layertype] then
1105                                if verbose > 0 then
1106                                    print("ignoring input panel creation")
1107                                end
1108                                return
1109                            end
1110                            command     = 0x10001
1111                       elseif oper == 2 then -- destroy
1112                            command     = 0x10002
1113                       elseif oper == 3 then  -- namechange
1114                            command     = 0x10009
1115                       elseif oper == 4 or oper == 5 then --visible or configure
1116                            local icolayer = ico_layer_type[win.layertype]
1117                            command     = 0x10008
1118                            arg.zone    = win.area
1119                            arg.node    = win.node
1120                            if icolayer then
1121                                arg.layertype = icolayer
1122                            end
1123                            arg.layer   = win.layer
1124                            arg.pos_x   = win.pos_x
1125                            arg.pos_y   = win.pos_y
1126                            arg.width   = win.width
1127                            arg.height  = win.height
1128                            arg.raise   = win.raise
1129                            arg.visible = win.visible
1130                            if win.active == 0 then
1131                                arg.active = 0
1132                            else
1133                                arg.active = 1
1134                            end
1135                       elseif oper == 6 then -- active
1136                            if win.active == 0 then
1137                                if verbose > 0 then
1138                                    print("ignoring inactive event")
1139                                end
1140                                return
1141                            end
1142                            command = 0x10006
1143                       elseif oper == 7 then -- map
1144                            local map = win.map
1145                            if not map then
1146                                return
1147                            end
1148                            if win.mapped == 0 then
1149                                command = 0x10012
1150                            else
1151                                command = 0x10011
1152                            end
1153                            arg.attr = map.type
1154                            arg.name = map.target
1155                            arg.width = map.width
1156                            arg.height = map.height
1157                            arg.stride = map.stride
1158                            arg.format = map.format
1159                       else
1160                            if verbose > 0 then
1161                                print("### nothing to do")
1162                            end
1163                            return
1164                       end
1165
1166                       local msg = m:JSON({ command = command,
1167                                            appid   = win.appid,
1168                                            pid     = win.pid,
1169                                            arg     = arg
1170                       })
1171                       if verbose > 0 then
1172                           print("### <== sending " ..
1173                                 command_name(msg.command) ..
1174                                 " window message to '" .. win.name .. "'")
1175                           if verbose > 1 then
1176                               print(msg)
1177                           end
1178                       end
1179                       sc:send_message(homescreen, msg)
1180
1181                       if oper == 1 then -- create
1182                           local i = input_layer[win.layertype]
1183                           local p = self:application(win.appid)
1184                           local s = p.privileges.screen
1185
1186                           if s == "system" then
1187                               local a = animation({})
1188                               local r = m:JSON({surface = win.surface,
1189                                                 visible = 1,
1190                                                 raise   = 1})
1191                               self:window_request(r,a,0)
1192                           else
1193                               if i then
1194                                   if verbose > 0 then
1195                                       print("do not make resource for " ..
1196                                             "input window")
1197                                   end
1198                               else
1199                                   resclnt:resource_set_create("screen",
1200                                                                "driver",
1201                                                                win.appid,
1202                                                                win.surface)
1203                                   special_screen_sets[win.surface] = true
1204                               end
1205                           end
1206                       elseif oper == 2 then -- destroy
1207                           resclnt:resource_set_destroy("screen", win.surface)
1208                           special_screen_sets[win.surface] = nil
1209                       elseif oper == 6 then -- active
1210                           if win.active then
1211                               local i = input_layer[win.layertype]
1212                               local p = self:application(win.appid)
1213                               local s = p.privileges.screen
1214                               local surface = win.surface
1215                               if not i and s ~= "system" then
1216                                  resclnt:resource_set_acquire("screen",surface)
1217                                  resmgr:window_raise(win.appid, surface, 1)
1218                               end
1219                           end
1220                       end
1221                   end,
1222
1223   layer_update = function(self, oper, layer, mask)
1224                       if verbose > 0 then
1225                           print("### LAYER UPDATE:" ..
1226                                 layer_operation_name(oper) ..
1227                                 " mask: " .. tostring(mask))
1228                           if verbose > 1 then
1229                               print(layer)
1230                           end
1231                       end
1232                       if oper == 3 then -- visible
1233                          local command = 0x10022
1234                          local msg = m:JSON({
1235                                          command = command,
1236                                          appid = "",
1237                                          arg = m:JSON({layer = layer.id,
1238                                                        visible = layer.visible
1239                                          })
1240                                 })
1241                          if verbose > 0 then
1242                             print("### <== sending "..command_name(command)..
1243                                   " layer message")
1244                             if verbose > 1 then
1245                                 print(msg)
1246                             end
1247                          end
1248                          sc:send_message(homescreen, msg)
1249                       else
1250                            if verbose > 0 then
1251                                print("### nothing to do")
1252                            end
1253                       end
1254                  end,
1255
1256   output_update = function(self, oper, out, mask)
1257                       local idx = out.index
1258                       local defidx = self.output_order[idx+1]
1259                       if verbose > 0 then
1260                           print("### OUTPUT UPDATE:" .. oper ..
1261                                 " mask: "..tostring(mask))
1262                       end
1263                       if not defidx then
1264                           return
1265                       end
1266                       print(out)
1267                       local outdef = self.outputs[defidx+1]
1268                       if (oper == 1) then -- create
1269                           if outdef then
1270                               self:output_request(m:JSON({index = idx,
1271                                                           id    = outdef.id,
1272                                                           name  = outdef.name
1273                                                           }))
1274                           end
1275                       elseif (oper == 5) then -- done
1276                           local ads = outdef.areas
1277                           local on = outdef.name
1278                           if ads then
1279                               for name,ad in pairs(ads) do
1280                                   local can = wmgr:canonical_name(on.."."..name)
1281                                   local a = m:JSON({name   = name,
1282                                                     output = out.index})
1283                                   for fld,val in pairs(ad) do
1284                                       a[fld] = self:geometry(out.width,
1285                                                              out.height,
1286                                                              val)
1287                                    end
1288                                    self:area_create(a)
1289                                    resmgr:area_create(area[can], outdef.zone)
1290                               end
1291                           end
1292                       end
1293                   end
1294 }
1295
1296
1297 imgr = input_manager {
1298   inputs = {{ name = "G27 Racing Wheel",
1299               id = 0,
1300               switch = { [2] = {appid="org.tizen.ico.app-soundsample"      },
1301                          [3] = {appid="org.tizen.ico.homescreen", keycode=1},
1302                          [4] = {appid="org.tizen.ico.app-soundsample"      },
1303                          [5] = {appid="org.tizen.ico.homescreen", keycode=2}
1304              }}
1305   },
1306
1307   manager_update = function(self, oper)
1308                        if verbose > 0 then
1309                            print("### <== INPUT MANAGER UPDATE:" ..
1310                                  input_manager_operation_name(oper))
1311                        end
1312                    end,
1313
1314   input_update = function(self, oper, inp, mask)
1315                      if verbose > 0 then
1316                          print("### INPUT UPDATE:" ..
1317                                 input_operation_name(oper) ..
1318                                 " mask: " .. tostring(mask))
1319                           if verbose > 1 then
1320                               print(inp)
1321                           end
1322                       end
1323                  end,
1324   code_update  = function(self, oper, code, mask)
1325                      if verbose > 0 then
1326                          print("### CODE UPDATE: mask: " .. tostring(mask))
1327                          if verbose > 1 then
1328                              print(code)
1329                          end
1330                      end
1331                      local msg = m:JSON({ command = 1,
1332                                           appid = "org.tizen.ico.homescreen",
1333                                           arg = m:JSON({ device = code.device,
1334                                                          time = code.time,
1335                                                          input = code.input,
1336                                                          code = code.id,
1337                                                          state = code.state
1338                                            })
1339                      })
1340                      if verbose > 0 then
1341                          print("### <== sending " ..
1342                                command_name(msg.command) ..
1343                                " input message")
1344                          if verbose > 1 then
1345                              print(msg)
1346                          end
1347                      end
1348                      sc:send_message(homescreen, msg)
1349                  end
1350 }
1351
1352 sc = m:get_system_controller()
1353
1354 -- resource sets
1355 sets = {}
1356
1357 -- special screen resource sets
1358 -- TODO: just rewrite screen resource handling to use regular resource API
1359
1360 special_screen_sets = {}
1361
1362 -- user manager
1363 um = m:UserManager()
1364
1365 connected = false
1366 homescreen = ""
1367 onscreen = ""
1368
1369 -- these shoud be before wmgr:connect() is called
1370 if verbose > 0 then
1371    print("====== creating applications ======")
1372 end
1373 application {
1374     appid          = "default",
1375     area           = "Center.Full",
1376     privileges     = { screen = "none", audio = "none" },
1377     resource_class = "player",
1378     screen_priority = 0
1379 }
1380
1381 application {
1382     appid           = "weston",
1383     area            = "Center.Full",
1384     privileges      = { screen = "system", audio = "none" },
1385     resource_class  = "implicit",
1386     screen_priority = 30
1387 }
1388
1389 application {
1390     appid           = "org.tizen.ico.homescreen",
1391     area            = "Center.Full",
1392     windows         = { {'ico_hs_controlbarwindow', 'Center.Control'} },
1393     privileges      = { screen = "system", audio = "system" },
1394     resource_class  = "player",
1395     screen_priority = 20
1396 }
1397
1398 application {
1399     appid           = "org.tizen.ico.statusbar",
1400     area            = "Center.Status",
1401     privileges      = { screen = "system", audio = "none" },
1402     resource_class  = "player",
1403     screen_priority = 20
1404 }
1405
1406 application {
1407     appid           = "org.tizen.ico.onscreen",
1408     area            = "Center.Full",
1409     privileges      = { screen = "system", audio = "system" },
1410     resource_class  = "player",
1411     screen_priority = 20
1412 }
1413
1414 application {
1415     appid           = "org.tizen.ico.login",
1416     area            = "Center.Full",
1417     privileges      = { screen = "system", audio = "system" },
1418     resource_class  = "player",
1419     screen_priority = 20
1420 }
1421
1422 application {
1423     appid           = "org.tizen.ico.camera_left",
1424     area            = "Center.SysApp.Left",
1425     privileges      = { screen = "system", audio = "none" },
1426     requisites      = { screen = "blinker_left", audio = "none" },
1427     resource_class  = "player",
1428     screen_priority = 30
1429 }
1430
1431 application {
1432     appid           = "org.tizen.ico.camera_right",
1433     area            = "Center.SysApp.Right",
1434     privileges      = { screen = "system", audio = "none" },
1435     requisites      = { screen = "blinker_right", audio = "none" },
1436     resource_class  = "player",
1437     screen_priority = 30
1438 }
1439
1440 application {
1441     appid           = "net.zmap.navi",
1442     area            = "Center.Full",
1443     privileges      = { screen = "none", audio = "none" },
1444     resource_class  = "navigator",
1445     screen_priority = 30
1446 }
1447
1448 application {
1449     appid           = "GV3ySIINq7.GhostCluster",
1450     area            = "Center.Full",
1451     privileges      = { screen = "none", audio = "none" },
1452     resource_class  = "system",
1453     screen_priority = 30
1454 }
1455
1456 if sc then
1457     sc.client_handler = function (self, cid, msg)
1458         local command = msg.command
1459         local appid = msg.appid
1460         if verbose > 0 then
1461             print('### ==> client handler:')
1462             if verbose > 1 then
1463                 print(msg)
1464             end
1465         end
1466
1467         -- known commands: 1 for SEND_APPID, synthetic command 0xFFFF for
1468         -- disconnection
1469
1470         if command == 0xFFFF then
1471             if verbose > 1 then
1472                 print('client ' .. cid .. ' disconnected')
1473             end
1474             if msg.appid == homescreen then
1475                 homescreen = ""
1476                 for i,v in pairs(special_screen_sets) do
1477                     resclnt:resource_set_destroy("screen", i)
1478                     special_screen_sets[i] = nil
1479                 end
1480             end
1481             return
1482         end
1483
1484         -- handle the connection to weston
1485
1486         if appid then
1487             if appid == "org.tizen.ico.homescreen" then
1488                 print('Setting homescreen='..appid)
1489                 homescreen = appid
1490                 if command and command == 1 then
1491                     send_driving_mode_to_home_screen()
1492                     send_night_mode_to_home_screen()
1493                 end
1494             elseif appid == "org.tizen.ico.onscreen" then
1495                 onscreen = appid
1496             end
1497
1498             if not connected and appid == "org.tizen.ico.homescreen" then
1499                 print('Trying to connect to weston...')
1500                 connected = wmgr:connect()
1501             end
1502         end
1503     end
1504
1505     sc.generic_handler = function (self, cid, msg)
1506         if verbose > 0 then
1507             print('### ==> generic handler:')
1508             if verbose > 1 then
1509                print(msg)
1510             end
1511         end
1512     end
1513
1514     sc.window_handler = function (self, cid, msg)
1515         if verbose > 0 then
1516             print('### ==> received ' ..
1517                    command_name(msg.command) .. ' message')
1518             if verbose > 1 then
1519                 print(tostring(msg))
1520             end
1521         end
1522
1523         local a = animation({})
1524         local nores = false
1525         if msg.command == 0x10003 then       -- ico SHOW command
1526             local raise_mask = 0x01000000
1527             local lower_mask = 0x02000000
1528             local nores_mask = 0x40000000
1529             local time_mask  = 0x00ffffff
1530             msg.arg.visible = 1
1531             if msg.arg then
1532                 local time = 200
1533                 if  msg.arg.anim_time then
1534                     local t = msg.arg.anim_time
1535                     time = m:AND(t, time_mask)
1536                     nores = not m:AND(t, nores_mask)
1537                     if m:AND(t, raise_mask) then
1538                         msg.arg.raise = 1
1539                     elseif m:AND(t, lower_mask) then
1540                         msg.arg.raise = 0
1541                     end
1542                 end
1543                 if msg.arg.anim_name then
1544                     a.show = { msg.arg.anim_name, time }
1545                     print('time: ' .. tostring(a.show[2]))
1546                 end
1547             end
1548             if not nores then
1549                 local p = wmgr:application(msg.appid)
1550                 local s = p.privileges.screen
1551                 if s == "system" then
1552                     nores = true
1553                     if not msg.arg.raise then
1554                         msg.arg.raise = 1
1555                     end
1556                 end
1557             end
1558             if verbose > 2 then
1559                 print('### ==> SHOW')
1560                 print(tostring(msg.arg))
1561             end
1562             if nores then
1563                 wmgr:window_request(msg.arg, a, 0)
1564             else
1565                 local surface = msg.arg.surface
1566                 resclnt:resource_set_acquire("screen", surface)
1567                 resmgr:window_raise(msg.appid, surface, 1)
1568             end
1569         elseif msg.command == 0x10004 then   -- ico HIDE command
1570             local raise_mask = 0x01000000
1571             local lower_mask = 0x02000000
1572             local nores_mask = 0x40000000
1573             local time_mask  = 0x00ffffff
1574             msg.arg.visible = 0
1575             if msg.arg then
1576                 local time = 200
1577                 if msg.arg.anim_time then
1578                     local t = msg.arg.anim_time
1579                     time = m:AND(t, time_mask)
1580                     nores = not m:AND(t, nores_mask)
1581                 end
1582                 if msg.arg.anim_name then
1583                     a.hide = { msg.arg.anim_name, time }
1584                     print('hide animation time: ' .. tostring(a.hide[2]))
1585                 end
1586             end
1587             if not nores then
1588                 local p = wmgr:application(msg.appid)
1589                 local s = p.privileges.screen
1590                 if s == "system" then
1591                     nores = true
1592                     msg.arg.raise = 0
1593                 end
1594             end
1595             if verbose > 2 then
1596                 print('### ==> HIDE REQUEST')
1597                 print(tostring(msg.arg))
1598             end
1599             if nores then
1600                 wmgr:window_request(msg.arg, a, 0)
1601             else
1602                 resmgr:window_raise(msg.appid, msg.arg.surface, -1)
1603             end
1604         elseif msg.command == 0x10005 then   -- ico MOVE
1605             if verbose > 2 then
1606                 print('### ==> MOVE REQUEST')
1607                 print(tostring(msg.arg))
1608             end
1609             if msg.arg.zone then
1610                 msg.arg.area = msg.arg.zone
1611             end
1612             wmgr:window_request(msg.arg, a, 0)
1613             -- TODO: handle if area changed
1614         elseif msg.command == 0x10006 then   -- ico ACTIVE
1615             if not msg.arg.active then
1616                 msg.arg.active = 3 -- pointer + keyboard
1617             end
1618             if verbose > 2 then
1619                 print('### ==> ACTIVE REQUEST')
1620                 print(tostring(msg.arg))
1621             end
1622             wmgr:window_request(msg.arg, a, 0)
1623         elseif msg.command == 0x10007 then   -- ico CHANGE_LAYER
1624             if verbose > 2 then
1625                 print('### ==> CHANGE_LAYER REQUEST')
1626                 print(tostring(msg.arg))
1627             end
1628             --[[
1629             if msg.arg.layer ~= 4 or msg.arg.layer ~= 5 then
1630                 print("do not change layer for other than cursor or touch")
1631                 return
1632             end
1633             --]]
1634             wmgr:window_request(msg.arg, a, 0)
1635         elseif msg.command == 0x10011 then   -- ico MAP_THUMB
1636             local framerate = msg.arg.framerate
1637             if not framerate or framerate < 0 then
1638                 framerate = 0
1639             end
1640             msg.arg.mapped = 1
1641             if verbose > 2 then
1642                 print('### ==> MAP_THUMB REQUEST')
1643                 print(msg.arg)
1644                 print('framerate: '..framerate)
1645             end
1646             wmgr:window_request(msg.arg, a, framerate)
1647         elseif msg.command == 0x10012 then   -- ico UNMAP_THUMB
1648             msg.arg.mapped = 0
1649             if verbose > 2 then
1650                 print('### ==> UNMAP_THUMB REQUEST')
1651                 print(msg.arg)
1652             end
1653             wmgr:window_request(msg.arg, a, 0)
1654         elseif msg.command == 0x10013 then -- ico MAP_BUFFER command
1655             local shmname = msg.arg.anim_name
1656             local bufsize = msg.arg.width
1657             local bufnum  = msg.arg.height
1658             if shmname and bufsize and bufnum then
1659                 if verbose > 2 then
1660                     print('### ==> MAP_BUFFER REQUEST')
1661                     print("shmaname='" .. shmname ..
1662                           "' bufsize='" .. bufsize ..
1663                           " bufnum=" .. bufnum)
1664                 end
1665                 wmgr:buffer_request(shmname, bufsize, bufnum)
1666             end
1667         elseif msg.command == 0x10020 then   -- ico SHOW_LAYER command
1668             msg.arg.visible = 1
1669             if verbose > 2 then
1670                 print('### ==> SHOW_LAYER REQUEST')
1671                 print(msg.arg)
1672             end
1673             wmgr:layer_request(msg.arg)
1674         elseif msg.command == 0x10021 then   -- ico HIDE_LAYER command
1675             msg.arg.visible = 0
1676             if verbose > 2 then
1677                 print('### ==> HIDE_LAYER REQUEST')
1678                 print(msg.arg)
1679             end
1680             wmgr:layer_request(msg.arg)
1681         end
1682     end
1683
1684     sc.input_handler = function (self, cid, msg)
1685         if verbose > 0 then
1686             print('### ==> input handler: ' .. command_name(msg.command))
1687             if verbose > 1 then
1688                 print(msg)
1689             end
1690         end
1691         if msg.command == 0x20001 then -- add_input
1692             msg.arg.appid = msg.appid
1693             if verbose > 2 then
1694                 print('### ==> ADD_INPUT REQUEST')
1695                 print(tostring(msg.arg))
1696             end
1697             imgr:input_request(msg.arg)
1698         elseif msg.command == 0x20002 then -- del_input
1699             msg.arg.appid = ''
1700             if verbose > 2 then
1701                 print('### ==> DEL_INPUT REQUEST')
1702                 print(tostring(msg.arg))
1703             end
1704             imgr:input_request(msg.arg)
1705         elseif msg.command == 0x20003 then -- send_input
1706         end
1707     end
1708
1709     sc.user_handler = function (self, cid, msg)
1710         if verbose > 0 then
1711             print('### ==> user handler: ' .. command_name(msg.command))
1712             if verbose > 1 then
1713                 print(msg)
1714             end
1715         end
1716
1717         if not um then
1718             print("User Manager not initialized")
1719             return
1720         end
1721
1722         if msg.command == 0x00030001 then -- MSG_CMD_CHANGE_USER
1723             print("command CHANGE_USER")
1724             if not msg.arg then
1725                 print("invalid message")
1726                 return
1727             end
1728
1729             username = msg.arg.user
1730             passwd = msg.arg.pass
1731
1732             if not username then
1733                 username = ""
1734             end
1735
1736             if not passwd then
1737                 passwd = ""
1738             end
1739
1740             success = um:changeUser(username, passwd)
1741
1742             if not success then
1743                 reply = m.JSON({
1744                     appid = msg.appid,
1745                     command = cmd
1746                 })
1747                 if sc:send_message(msg.appid, reply) then
1748                     print('*** sent authentication failed message')
1749                 else
1750                     print('*** failed to send authentication failed message')
1751                 end
1752             end
1753
1754         elseif msg.command == 0x00030002 then -- MSG_CMD_GET_USERLIST
1755             print("command GET_USERLIST")
1756             if not msg.appid then
1757                 print("invalid message")
1758                 return
1759             end
1760
1761             users, currentUser = um:getUserList()
1762
1763             if not users then
1764                 print("failed to get user list")
1765                 return
1766             end
1767
1768             nUsers = 0
1769
1770             for i,v in pairs(users) do
1771                 nUsers = nUsers + 1
1772             end
1773
1774             if not currentUser then
1775                 currentUser = ""
1776             end
1777
1778             if verbose > 1 then
1779                 print("current user: " .. currentUser)
1780                 print("user list:")
1781                 for i,v in pairs(users) do
1782                     print(v)
1783                 end
1784             end
1785
1786             reply = m.JSON({
1787                 appid = msg.appid,
1788                 command = cmd,
1789                 arg = m.JSON({
1790                     user_num = nUsers,
1791                     user_list = users,
1792                     user_login = currentUser
1793                 })
1794             })
1795
1796             if verbose > 1 then
1797                 print("### <== GetUserList reply: " .. tostring(reply))
1798             end
1799
1800             if sc:send_message(msg.appid, reply) then
1801                 print('*** reply OK')
1802             else
1803                 print('*** reply FAILED')
1804             end
1805
1806         elseif msg.command == 0x00030003 then -- MSG_CMD_GET_LASTINFO
1807             print("command GET_LASTINFO")
1808             if not msg.appid then
1809                 print("invalid message")
1810                 return
1811             end
1812
1813             lastInfo = um:getLastInfo(msg.appid)
1814
1815             if not lastInfo then
1816                 print("failed to get last info for app" .. msg.appid)
1817                 return
1818             end
1819
1820             reply = m.JSON({
1821                 appid = msg.appid,
1822                 command = cmd,
1823                 arg = m.JSON({
1824                     lastinfo = lastinfo
1825                 })
1826             })
1827
1828             if sc:send_message(msg.appid, reply) then
1829                 print('*** reply OK')
1830             else
1831                 print('*** reply FAILED')
1832             end
1833
1834         elseif msg.command == 0x00030004 then -- MSG_CMD_SET_LASTINFO
1835             print("command SET_LASTINFO")
1836             if not msg.arg or not msg.appid then
1837                 print("invalid message")
1838                 return
1839             end
1840
1841             lastInfo = um:setLastInfo(msg.appid, msg.arg.lastinfo)
1842         end
1843     end
1844
1845     sc.resource_handler = function (self, cid, msg)
1846         if verbose > 0 then
1847             print('### ==> resource handler: ' .. command_name(msg.command))
1848             if verbose > 1 then
1849                 print(msg)
1850             end
1851         end
1852
1853         createResourceSet = function (ctl, client, msg)
1854             cb = function(rset, data)
1855                 print("> resource callback")
1856
1857                 -- type is either basic (0) or interrupt (1)
1858                 requestType = 0
1859                 if msg.res.type then
1860                     requestType = msg.res.type
1861                 end
1862
1863                 if rset.acquired then
1864                     cmd = 0x00040001 -- acquire
1865                 else
1866                     cmd = 0x00040002 -- release
1867                 end
1868
1869                 reply = m.JSON({
1870                         appid = data.client,
1871                         command = cmd,
1872                         res = {
1873                             type = requestType
1874                         }
1875                     })
1876
1877                 if rset.resources.audio_playback then
1878                     reply.res.sound = {
1879                         zone = "driver",
1880                         name = msg.appid,
1881                         adjust = 0,
1882                         -- id = "0"
1883                     }
1884                 end
1885
1886                 if rset.resources.display then
1887                     reply.res.window = {
1888                         zone = "driver",
1889                         name = msg.appid,
1890                         -- id = "0"
1891                     }
1892                 end
1893
1894                 if rset.resources.input then
1895                     reply.res.input = {
1896                         name = msg.appid,
1897                         event = 0
1898                     }
1899                 end
1900                 print("sending message to client: " .. data.client)
1901
1902                 if sc:send_message(data.client, reply) then
1903                     print('*** reply OK')
1904                 else
1905                     print('*** reply FAILED')
1906                 end
1907             end
1908
1909             rset = m:ResourceSet({
1910                     application_class = "player",
1911                     zone = "driver", -- msg.zone ("full")
1912                     callback = cb
1913                 })
1914
1915             rset.data = {
1916                 cid = cid,
1917                 ctl = ctl
1918             }
1919
1920             if msg.res.sound then
1921                 rset:addResource({
1922                         resource_name = "audio_playback"
1923                     })
1924                 rset.resources.audio_playback.attributes.pid = tostring(msg.pid)
1925                 rset.resources.audio_playback.attributes.appid = msg.appid
1926                 print("sound name: " .. msg.res.sound.name)
1927                 print("sound zone:" .. msg.res.sound.zone)
1928                 print("sound adjust: " .. tostring(msg.res.sound.adjust))
1929                 if msg.res.sound.id then
1930                     print("sound id: " .. msg.res.sound.id)
1931                 end
1932             end
1933
1934             if msg.res.input then
1935                 rset:addResource({
1936                         resource_name = "input"
1937                     })
1938                 rset.resources.input.attributes.pid = tostring(msg.pid)
1939                 rset.resources.input.attributes.appid = msg.appid
1940                 print("input name: " .. msg.res.sound.name)
1941                 print("input event:" .. tostring(msg.res.input.event))
1942             end
1943
1944             if msg.res.window then
1945                 rset:addResource({
1946                         resource_name = "display"
1947                     })
1948                 rset.resources.display.attributes.pid = tostring(msg.pid)
1949                 rset.resources.display.attributes.appid = msg.appid
1950                 print("display name: " .. msg.res.display.name)
1951                 print("display zone:" .. msg.res.display.zone)
1952                 if msg.res.display.id then
1953                     print("display id: " .. msg.res.display.id)
1954                 end
1955             end
1956
1957             return rset
1958         end
1959
1960         -- parse the message
1961
1962         -- fields common to all messages:
1963         --      msg.command
1964         --      msg.appid
1965         --      msg.pid
1966
1967         if msg.command == 0x00040011 then -- MSG_CMD_CREATE_RES
1968             print("command CREATE")
1969
1970             if not sets.cid then
1971                 sets.cid = createResourceSet(self, cid, msg)
1972             end
1973
1974         elseif msg.command == 0x00040012 then -- MSG_CMD_DESTORY_RES
1975             print("command DESTROY")
1976
1977             if sets.cid then
1978                 sets.cid:release()
1979             end
1980
1981             sets.cid = nil -- garbage collecting
1982
1983         elseif msg.command == 0x00040001 then -- MSG_CMD_ACQUIRE_RES
1984             print("command ACQUIRE")
1985
1986             if not sets.cid then
1987                 sets.cid = createResourceSet(self, cid, msg)
1988             end
1989
1990             sets.cid:acquire()
1991
1992         elseif msg.command == 0x00040002 then -- MSG_CMD_RELEASE_RES
1993             print("command RELEASE")
1994
1995             if sets.cid then
1996                 sets.cid:release()
1997             end
1998
1999         elseif msg.command == 0x00040003 then -- MSG_CMD_DEPRIVE_RES
2000             print("command DEPRIVE")
2001
2002         elseif msg.command == 0x00040004 then -- MSG_CMD_WAITING_RES
2003             print("command WAITING")
2004
2005         elseif msg.command == 0x00040005 then -- MSG_CMD_REVERT_RES
2006             print("command REVERT")
2007         end
2008     end
2009
2010     sc.inputdev_handler = function (self, cid, msg)
2011         if verbose > 0 then
2012             print('*** inputdev handler: ' .. command_name(msg.command))
2013             if verbose > 1 then
2014                 print(msg)
2015             end
2016         end
2017     end
2018 end
2019
2020 function send_driving_mode_to_home_screen()
2021     if homescreen == "" then
2022         return
2023     end
2024
2025     local driving_mode = mdb.select.select_driving_mode.single_value
2026
2027     if not driving_mode then driving_mode = 0 end
2028
2029     local reply = m:JSON({ command = 0x60001,
2030                            arg     = m:JSON({ stateid = 1,
2031                                               state   = driving_mode
2032                                      })
2033                   })
2034
2035     if verbose > 0 then
2036         print("### <== sending " .. command_name(reply.command) .. " message")
2037         if verbose > 1 then
2038             print(reply)
2039         end
2040     end
2041
2042     sc:send_message(homescreen, reply)
2043 end
2044
2045 function send_night_mode_to_home_screen()
2046     if homescreen == "" then
2047         return
2048     end
2049
2050     local night_mode = mdb.select.select_night_mode.single_value
2051
2052     if not night_mode then night_mode = 0 end
2053
2054     local reply = m:JSON({ command = 0x60001,
2055                            arg     = m:JSON({ stateid = 2,
2056                                               state   = night_mode
2057                                      })
2058                   })
2059
2060     if verbose > 0 then
2061         print("### <== sending " .. command_name(reply.command) .. " message")
2062         if verbose > 1 then
2063             print(reply)
2064         end
2065      end
2066
2067      sc:send_message(homescreen, reply)
2068 end
2069
2070 -- we should have 'audio_playback' defined by now
2071 m:try_load_plugin('telephony')